/*
"JSFXGEN" - Dialog.as
Modular Integrated Development Environment for JesuSonic
Project page: http://jsfxgen.googlecode.com

Copyright 2009 Lubomir I. Ivanov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package neolit123.ui
{
  // =========================================================
  // [ import
  // =========================================================  
  import flash.display.Sprite;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.text.TextFormat;
  import flash.text.TextFormatAlign;
  import flash.events.MouseEvent;
  // =========================================================
  // [ class
  // =========================================================
  public class Dialog extends Sprite
  {
    private var bkg:Box;
    private var panel:Sprite = new Sprite;
    private var panelbkg:Box;
    private var btn0:UIButton;
    private var btn1:UIButton;
    private var txt:TextField = new TextField;
    private var tf:TextFormat = new TextFormat("_sans",10,0x000000,true,null,null,null,null,"center");
    private var f0d:Function;
    private var f1d:Function;
    private const pw:Number = 300;
    // =========================================================
    // [ create loading screen
    // =========================================================
    public function Dialog(bkgcolor:int,panelbkgcolor:int,panelbordercolor:int,txtcolor:int):void
    {      
      // @ add bkg
      bkg = new Box(800,600,bkgcolor,bkgcolor);
      addChild(bkg);
      bkg.buttonMode = true;
      bkg.mouseChildren = false;
      bkg.useHandCursor = false;
      bkg.alpha = 0.5;
      // @ add panel
      addChild(panel);
      panelbkg = new Box(pw,50,panelbkgcolor,panelbordercolor);
      panel.addChild(panelbkg);
      // @ add txt to panel
      panel.addChild(txt);
      with (txt)
      {
        x = 10;
        y = 10;
        width = pw-20;
        autoSize = "center";
        defaultTextFormat = tf;
          textColor = txtcolor;
        selectable = false;
        multiline = true;
        wordWrap = true;
      }
      btn0 = new UIButton();
      btn1 = new UIButton();
      panel.addChild(btn0);
      panel.addChild(btn1);
      btn0.x = (pw - btn0.width*2 - 25)*0.5;
      btn1.x = btn0.x + btn0.width + 25;
      btn0.addEventListener(MouseEvent.CLICK, clickb0);
      btn1.addEventListener(MouseEvent.CLICK, clickb1);
      function clickb0(e:MouseEvent = null):void 
      {
         f0d();
      }
      function clickb1(e:MouseEvent = null):void 
      {
         f1d();
      }
      visible = false;
    }
    // =========================================================
    // [ update text
    // =========================================================
    public function update(str:String,b0:String,b1:String,f0:Function,f1:Function):void
    {
      txt.text = str;
      f0d = f0;
      f1d = f1;
      btn0.setlabel(b0);
      btn1.setlabel(b1);
      if (b1 == "---") btn1.enable(false) else btn1.enable(true);
      if (!visible) visible = true;
      align(bkg.width, bkg.height);
      Main.debug.df("Open dialog - "+b0+", "+b1, false);
    }
    // =========================================================
    // [ align to stage
    // =========================================================
    public function align(sw:Number, sh:Number):void
    {
      bkg.width = sw;
      bkg.height = sh;
      panelbkg.height = txt.height + btn0.height + 30;
      btn0.y = btn1.y = txt.height + 20;
      panel.x = (sw - pw)*0.5;
      panel.y = (sh - panelbkg.height)*0.5;
    }
    // =========================================================
    // [ dialog close
    // =========================================================
    public function close():void
    {
      visible = false;
    }
  }
}